package com.zhika.zishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.SearchAdapter;
import com.zhika.zishi.beans.SearchBean;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/20.
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "SearchActivity";

    private ListView listView;

    private ArrayList<SearchBean> searchList = new ArrayList<>();

    private SearchAdapter adapter;

    //搜索时的类型，已选好的
    private TableRow selectRow;

    private TextView selectText;

    private LinearLayout typeLayout;

    private TableRow contentRow;

    private TableRow userRow;

    private TextView cancelText;

    private LinearLayout addProblemLayout;

    private EditText problemEdit;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);

        addTestData();

        initView();
    }

    private void initView(){
        listView = (ListView) findViewById(R.id.search_list_view);
        adapter = new SearchAdapter(SearchActivity.this,searchList);
        listView.setAdapter(adapter);

        selectRow = (TableRow) findViewById(R.id.search_select_row);
        selectRow.setOnClickListener(this);
        selectText = (TextView) findViewById(R.id.search_select_text);

        typeLayout =(LinearLayout) findViewById(R.id.search_type_layout);
        contentRow = (TableRow) findViewById(R.id.search_content_row);
        contentRow.setOnClickListener(this);
        userRow = (TableRow) findViewById(R.id.search_user_row);
        userRow.setOnClickListener(this);

        cancelText = (TextView) findViewById(R.id.search_cancel_text);
        cancelText.setOnClickListener(this);

        addProblemLayout = (LinearLayout) findViewById(R.id.search_add_problem_layout);
        addProblemLayout.setOnClickListener(this);

        problemEdit = (EditText) findViewById(R.id.search_problem_edit);
    }

    private void addTestData(){
        for (int i = 0;i<3;i++){
            SearchBean bean = new SearchBean();
            bean.setAnswerType("13个答复");
            bean.setContentTitle("与关键字相关的攻略或问题");

            searchList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_select_row:
                typeLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.search_content_row:
                contentUserRowListener("内容");
                break;
            case R.id.search_user_row:
                contentUserRowListener("用户");
                break;
            case R.id.search_cancel_text:
                finish();
                break;
            case R.id.search_add_problem_layout:
                addProblemLayoutListener();
                break;
            default:
                break;
        }
    }

    /**
     * 内容和用户的点击事件
     * @param type
     */
    private void contentUserRowListener(String type){
        selectText.setText(type);
        typeLayout.setVisibility(View.GONE);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogUtil.e(TAG,"onTouchEvent事件");
        typeLayout.setVisibility(View.GONE);
        return super.onTouchEvent(event);
    }

    private void addProblemLayoutListener(){
        String problemStr = problemEdit.getText().toString();
        Intent intent = new Intent(SearchActivity.this,QuizActivity.class);
        if (problemStr != null){
            intent.putExtra("problemtitle",problemStr);
        }
        startActivity(intent);
    }
}
